package com.mall.controller;

import com.mall.domain.ItemCat;
import com.mall.domain.ItemGoods;
import com.mall.domain.Page;
import com.mall.service.IItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品类别web层
 * @author : liangY
 * @date : 2021-01-04 16:57
 */
@Controller
@RequestMapping("/sorts")
public class ItemCatController {

    @Autowired
    private IItemCatService iItemCatService;

    /**
     * 查询所有商品分类
     * @param page
     * @return
     */
    @RequestMapping("/findAll")
    public @ResponseBody
    Map<String, Object> findAll(@RequestBody Page page){
        Map<String,Object> sortMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        //获取会员集合
        List<ItemCat> sorts = iItemCatService.findAll(page);
        //放入dataMap集合
        dataMap.put("sorts",sorts);
        dataMap.put("total",iItemCatService.findTotal(page.getCat_level()));
        //放入Map集合
        sortMap.put("data",dataMap);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        metaMap.put("msg","获取分类列表成功");
        metaMap.put("status",200);

        sortMap.put("meta",metaMap);
        return sortMap;
    }

    /**
     * 更新指定商品类别
     * @param itemCat
     * @return
     */
    @RequestMapping("/update")
    public @ResponseBody
    Map<String, Object> update(@RequestBody ItemCat itemCat){
        Map<String,Object> sortMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        //放入dataMap集合
        dataMap.put("sorts",itemCat);
        //放入Map集合
        sortMap.put("data",dataMap);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        int flag = iItemCatService.update(itemCat);
        if(flag > 0){
            metaMap.put("msg","更新商品分类成功");
            metaMap.put("status",200);
        } else {
            metaMap.put("msg","更新商品分类失败");
            metaMap.put("status",0);
        }
        sortMap.put("meta",metaMap);
        return sortMap;
    }

    /**
     * 添加商品分类
     * @param itemCat
     * @return
     */
    @RequestMapping("/add")
    public @ResponseBody
    Map<String, Object> add(@RequestBody ItemCat itemCat){
        Map<String,Object> sortMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        //放入dataMap集合
        dataMap.put("sorts",itemCat);
        //放入Map集合
        sortMap.put("data",dataMap);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        int flag = iItemCatService.add(itemCat);
        if(flag > 0){
            metaMap.put("msg","添加商品分类成功");
            metaMap.put("status",200);
        } else {
            metaMap.put("msg","更新商品分类失败");
            metaMap.put("status",0);
        }
        sortMap.put("meta",metaMap);
        return sortMap;
    }

    /**
     * 删除商品分类
     * @param id
     * @return
     */
    @RequestMapping("/del")
    public @ResponseBody
    Map<String, Object> add(Integer id){
        Map<String,Object> sortMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        //放入Map集合
        sortMap.put("data",dataMap);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        int flag = iItemCatService.del(id);
        if(flag > 0){
            metaMap.put("msg","删除商品分类成功");
            metaMap.put("status",200);
        } else {
            metaMap.put("msg","删除商品分类失败");
            metaMap.put("status",0);
        }
        sortMap.put("meta",metaMap);
        return sortMap;
    }
    @RequestMapping("/del1")
    public @ResponseBody
    Map<String, Object> del(Integer id){
        Map<String,Object> sortMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        //放入Map集合
        sortMap.put("data",dataMap);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        int flag = iItemCatService.del1(id);
        if(flag > 0){
            metaMap.put("msg","删除商品分类成功");
            metaMap.put("status",200);
        } else {
            metaMap.put("msg","删除商品分类失败");
            metaMap.put("status",0);
        }
        sortMap.put("meta",metaMap);
        return sortMap;
    }

}
